package travellersalesman;

import java.io.IOException;
import java.io.StreamCorruptedException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Random;

import jcuda.driver.CUfunction;

import stochastic.Solution;

public class TravellerPath 
		implements 	Solution
{
	private ExtendetGraph obj;
	private ArrayList<Integer> path;
	
	public TravellerPath(ExtendetGraph g) {
		this.obj = g;
		path = new ArrayList<Integer>();
		for(int i=0; i<obj.getDimension(); ++i) {
			path.add(i);
		}
		normalize();
	}
	private void normalize(int left, int right) {
		ArrayList<Integer> result = new ArrayList<Integer>();
		for(int x=Math.max(left-1, 0);x<=right; ++x) {
			int i = path.get(x);
			int j = path.get((x+1)%path.size());
			result.addAll(obj.getPath(i, j));
			result.remove(result.size()-1);		//result.pop()
		}
				
		ArrayList<Integer> temp = new ArrayList<Integer>();
		ArrayList<Boolean> isUsed = new ArrayList<Boolean>();
		for(int x = 0; x<path.size(); ++x) {
			isUsed.add(false);
		}
		for(int i = 0; temp.size() != path.size(); ++i) {
			if(isUsed.get(result.get(i)) != true) {
				temp.add(result.get(i));
				isUsed.set(result.get(i), true);
			}
		}
		assert (temp.size()==path.size()):temp.size();
		path = temp;
	}
	private void normalize() {
		
		normalize(0, path.size()-1);
	}

	@Override
	public double getCost() {
		double result = 0;
		for(int x=0;x<path.size();++x) {
			result += obj.getDistance(path.get(x), path.get((x+1)%path.size()));
		}
		return result;
	}

	@Override
	public int getDegreeOfFreedom() {
		return path.size();
	}

	@Override
	public Solution perturb() {
		Random rand = new Random();
		TravellerPath next = this.clone();
		int a = rand.nextInt(getDegreeOfFreedom());
		int b = rand.nextInt(getDegreeOfFreedom());
		int temp = next.path.get(a);
		next.path.set(a, next.path.get(b));
		next.path.set(b, temp);
		return next;
	}
	
	public TravellerPath clone() {
		TravellerPath result = new TravellerPath(obj);
		result.path = (ArrayList<Integer>)this.path.clone();
		return result;
	}
	
	public String toString() {
		ArrayList<Integer> temp = new ArrayList<Integer>();
		for(int x=0;x<path.size(); ++x) {
			int i = path.get(x);
			int j = path.get((x+1)%path.size());
			temp.addAll(obj.getPath(i, j));
			temp.remove(temp.size()-1);		//result.pop()
		}
		
		StringBuffer result = new StringBuffer();
		result.append('[');
		for (Integer elem : temp) {
			result.append(elem);
			result.append(", ");
		}
		result.append(temp.get(0));
		result.append(']');
		
		return result.toString();
		
		
	}
	
	public Integer[] getPath() {
		ArrayList<Integer> temp = new ArrayList<Integer>();
		for(int x=0;x<path.size(); ++x) {
			int i = path.get(x);
			int j = path.get((x+1)%path.size());
			temp.addAll(obj.getPath(i, j));
			temp.remove(temp.size()-1);		//result.pop()
		}
		
		Integer[] result = (Integer[]) temp.toArray(new Integer[temp.size()]);
		return result;
		
		
	}
	
	public ExtendetGraph getGraph() {
		return obj;
	}
	@Override
	public void setParam() {
		// TODO Auto-generated method stub
		
	}
	@Override
	public String getKernelFile() {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public void cudaClean() {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void loadCUDA(int maxThreads, int numPerturb, double t)
			throws IOException {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void launchKernel() {
		// TODO Auto-generated method stub
		
	}
	@Override
	public Solution cudaChoiceSolution(int numThreads, int ok) {
		// TODO Auto-generated method stub
		return null;
	}
	

}
